package tw.com.hitevision.whiteboard.android.render;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PointF;

import java.util.List;

import tw.com.hitevision.whiteboard.android.state.HighlighterProperty;
import tw.com.hitevision.whiteboard.android.state.LineProperty;
import tw.com.hitevision.whiteboard.android.state.PenProperty;

public class RulerHelper {
    private Paint strokePaint;
    private LineProperty lineProperty;
    private List<PointF> points;
    private Path path;

    public RulerHelper(List<PointF> points, LineProperty lineProperty, float scale) {
        strokePaint = new Paint();
        strokePaint.setStyle(Paint.Style.STROKE);
        strokePaint.setStrokeCap(Paint.Cap.ROUND);
        strokePaint.setStrokeJoin(Paint.Join.ROUND);
        strokePaint.setStyle(Paint.Style.STROKE);
        strokePaint.setAntiAlias(true);
        if (lineProperty instanceof PenProperty) {
            strokePaint.setStrokeWidth(((PenProperty) lineProperty).getStrokeWidth() * scale);
            strokePaint.setColor(((PenProperty) lineProperty).getColor());
        } else if (lineProperty instanceof HighlighterProperty) {
            strokePaint.setStrokeWidth(((HighlighterProperty) lineProperty).getStrokeWidth() * scale);
            strokePaint.setColor(((HighlighterProperty) lineProperty).getColor());
        }
        this.points = points;
        this.lineProperty = lineProperty;
        path = new Path();
    }

    public void setPoints(List<PointF> points) {
        this.points = points;
        path.rewind();
        path.moveTo(points.get(0).x, points.get(0).y);
        for (int i = 1;i < points.size();i++) {
            path.lineTo(points.get(i).x, points.get(i).y);
        }
    }

    public void render(Canvas canvas) {
        canvas.drawPath(path, strokePaint);
    }

    public Line createLine(Context context, float canvasRatio) {
        Line line = null;
        if (lineProperty instanceof PenProperty) {
            line = ((PenProperty) lineProperty).create(context, points.get(0).x, points.get(0).y, canvasRatio);
        } else if (lineProperty instanceof HighlighterProperty) {
            line = ((HighlighterProperty) lineProperty).create(context, points.get(0).x, points.get(0).y, canvasRatio);
        } else {
            return null;
        }
        final float minDis = line.getFilterDistance() * 2;// diameter

        for (int i = 1; i < points.size(); i++) {
            PointF start = points.get(i - 1);
            PointF end = points.get(i);
            float vLength = PointF.length(end.x - start.x, end.y - start.y);
            PointF v = new PointF((end.x - start.x) / vLength, (end.y - start.y) / vLength);

            int count = (int) Math.floor(vLength / minDis);
            float px = start.x;
            float py = start.y;
            for (int ii = 0;ii < count;ii ++) {
                px += v.x * minDis;
                py += v.y * minDis;
                line.add(px, py);
            }

            line.add(end.x, end.y);
        }

        return line;
    }
}
